package com.qujia.fleet.controller;

import com.qujia.common.annotation.Log;
import com.qujia.common.core.controller.BaseController;
import com.qujia.common.core.domain.AjaxResult;
import com.qujia.common.core.domain.model.LoginUser;
import com.qujia.common.core.page.TableDataInfo;
import com.qujia.common.enums.BusinessType;
import com.qujia.common.utils.SecurityUtils;
import com.qujia.fleet.domain.form.FleetCardForm;
import com.qujia.fleet.domain.form.FleetCardQueryForm;
import com.qujia.fleet.domain.form.FleetCardRechargeForm;
import com.qujia.fleet.domain.vo.FleetCardVo;
import com.qujia.fleet.enums.CardRechargeRecordEnum;
import com.qujia.fleet.service.FleetCardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

@Api(tags = "车队卡管理")
@RestController
@RequestMapping("/fleet/card")
public class FleetCardController extends BaseController {

    @Autowired
    private FleetCardService fleetCardService;

    @Log(title = "车队卡", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增车队卡", notes = "新增车队卡")
    @PostMapping
    public AjaxResult add(@Valid @RequestBody FleetCardForm fleetCardForm) {
        LoginUser loginUser= SecurityUtils.getLoginUser();
        Long enterpriseId = loginUser.getUser().getEnterpriseId();
        fleetCardForm.setEnterpriseId(enterpriseId);
        return toAjax(fleetCardService.insertFleetCard(fleetCardForm));
    }

    @Log(title = "车队卡", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改车队卡", notes = "修改车队卡")
    @PutMapping("/{id}")
    public AjaxResult edit(@PathVariable Long id, @RequestBody FleetCardForm fleetCardForm) {
        return toAjax(fleetCardService.updateFleetCard(id, fleetCardForm));
    }

    @Log(title = "车队卡", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "车队卡充值", notes = "车队卡充值")
    @PutMapping("/recharge/{id}")
    public AjaxResult recharge(@PathVariable Long id, @RequestBody FleetCardRechargeForm fleetCardRechargeForm) {
        fleetCardRechargeForm.setCardType(CardRechargeRecordEnum.FLEET_RECHARGE.getValue());
        fleetCardRechargeForm.setIsPush("0");
        return toAjax(fleetCardService.updateFleetCardRecharge(id, fleetCardRechargeForm));
    }

    @ApiOperation(value = "车队卡分页列表", notes = "车队卡分页列表")
    @GetMapping("/page")
    public TableDataInfo page(FleetCardQueryForm fleetCardQueryForm) {
        startPage();
        List<FleetCardVo> list = fleetCardService.selectFleetCardList(fleetCardQueryForm);
        return getDataTable(list);
    }

    @Log(title = "车队卡", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "车队卡配置子卡的分配方式", notes = "车队卡配置子卡的分配方式")
    @PutMapping("/allocationType/{allocationType}/id/{id}")
    public AjaxResult allocationType(@PathVariable Integer allocationType, @PathVariable Long id) {
        return toAjax(fleetCardService.updateAllocationType(allocationType, id));
    }

    @Log(title = "车队卡", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "主卡回收子卡", notes = "主卡回收子卡")
    @PutMapping("/recovery/{id}")
    public AjaxResult recovery(@PathVariable Long id) {
        return toAjax(fleetCardService.recovery(id));
    }

    @Log(title = "车队卡", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "删除车队卡", notes = "删除车队卡")
    @DeleteMapping("/{ids}")
    public AjaxResult deleted(@PathVariable List<Long> ids) {
        return toAjax(fleetCardService.deleted(ids));
    }

    @ApiOperation(value = "获取主卡信息", notes = "获取主卡信息")
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable Long id) {
        return AjaxResult.success(fleetCardService.getById(id));
    }

    @ApiOperation(value = "车队卡添加调整余额", notes = "车队卡添加调整余额",httpMethod = "POST")
    @PostMapping("/adjust/balance")
    public AjaxResult adjustBalance(@RequestBody FleetCardRechargeForm fleetCardRechargeForm){
        if(Objects.isNull(fleetCardRechargeForm) || Objects.isNull(fleetCardRechargeForm.getFleetCardId())){
            return AjaxResult.error("请求参数异常");
        }
        if(Objects.isNull(fleetCardRechargeForm.getStationId())){
            return AjaxResult.error("请求选择油站");
        }
        int i = fleetCardService.updateFleetCardRecharge(fleetCardRechargeForm.getFleetCardId(), fleetCardRechargeForm);
        if(i <= 0){
            return AjaxResult.error("调整余额失败");
        }
        return AjaxResult.success(i);
    }

}
